const express = require('express')
const http = require('http')
const MsgRouter = require('./src/router/msg')
const socketIo = require('./src/socket/index')
// -------------数据库 原代码-----------
//https://www.sequelize.cn/core-concepts/model-basics
// const { Sequelize, DataTypes } = require('sequelize');
//--------------封装抽离----------------
const cors = require('cors')
const app = express()
const server = http.Server(app)
const io = new socketIo(server, {
  cors: true,
})
app.use(cors())
app.use(function (req, res, next) {
  res.io = function (emit, data = null, type = 'answer', id = null) {
    if (id) {
      io.to(id).emit(type, data)
    } else {
      io.to(id).emit(type, data)
    }
  }
  res.cc = function (data, status = 100) {
    res.send({
      status,
      data: data.data,
      message: data.msg instanceof Error ? err.msg : 'success',
    })
  }
  next()
})
app.use('/msg', MsgRouter)

// 静态部署
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/html/index.html')
})

const online = new Set()

server.listen(9090, () => {
  console.log(`Server is running on port http://localhost:${9090}`)
})
